package com.lx.nba.parse.impl;

import com.lx.nba.util.Content;
import com.lx.nba.util.Context;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.lx.nba.dao.TeamDao;
import com.lx.nba.entiy.Team;
import com.lx.nba.parse.ContentParse;
import com.lx.nba.util.Result;

public class TeamParseImpl implements ContentParse{

	@Autowired
	private TeamDao teamDao ;
	
	
	@Override
	public Result parse( Content content ) {
		// TODO Auto-generated method stub
		
		List<Team> listTeams = new ArrayList<>() ;
		
		Document doc = Jsoup.parse(content.getContent()) ;
		
		Elements teamList = doc.getElementsByClass("teams-list") ;
		
		for( Element teams:teamList){
			
			Elements aList = teams.getElementsByClass("team-logo-40") ;
			for(Element a:aList){
				
				String href = a.attr("href") ;
				
				Team team = new Team() ;
				
				for( int i = 0 ; i < href.length() ; i++){
					
					if( href.charAt(i) == '=' ){
						
						team.setName(href.substring(i+1));
						listTeams.add(team);
					}
					
				}
				
				
				
			}
			
		}
		
		if( !listTeams.isEmpty() ){
			
			teamDao.saveTeams(listTeams);
			Context.INIT_TEAM = true ;
			
		}
		
		return null;
	}
	

}
